package com.zhiwang.planet.ui.my

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.zhiwang.planet.R
import com.zhiwang.planet.di.component.DaggerAccountComponent
import com.zhiwang.planet.di.module.AccountModule
import com.zhiwang.planet.di.module.AutoDisposeModule
import com.zhiwang.planet.event.DiamondChangeEvent
import com.zhiwang.planet.mvp.p.UserPresenter
import com.zhiwang.planet.mvp.v.DiamondBeanInfoView
import com.zhiwang.planet.mvvm.model.StringHolder
import com.zhiwang.planet.mvvm.model.entity.UserDao
import com.zhiwang.planet.mvvm.model.entity.main.DiamondBeanInfo
import com.zhiwang.planet.ui.BaseActivity
import com.zhiwang.planet.ui.StatusbarUtil
import com.zhiwang.planet.ui.my.withdraw.AliWithdrawFragment
import com.zhiwang.planet.ui.my.withdraw.WithDrawRecordFragment
import com.zhiwang.planet.ui.my.withdraw.WxWithdrawFragment
import com.zhiwang.planet.util.*
import com.zhiwang.planet.widget.SimpleDialogBuilder
import kotlinx.android.synthetic.main.activity_with_draw.*
import org.greenrobot.eventbus.EventBus
import javax.inject.Inject

//我的明细（withdraw_btn_bg）
class WithdrawActivity : BaseActivity(), DiamondBeanInfoView {

    private var cashCount: Float = 0f
    @Inject
    lateinit var userDao: UserDao

    @Inject
    lateinit var userPresenter: UserPresenter


    val tabStates = arrayListOf<TabState>()

    val fragments = arrayListOf<Fragment>()

    override fun initInjection() {
        DaggerAccountComponent.builder()
                .appComponent(getAppComponent())
                .accountModule(AccountModule(this))
                .autoDisposeModule(AutoDisposeModule(this))
                .build()
                .inject(this)
        userPresenter.diamondBeanInfoView = this
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_with_draw)

        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        val user = userDao.getFirstUser()
        val aliBind = user.bindInfo.isAliBind
//        val weixinBind = user.bindInfo.isWeiXinBind
        //设置状态栏颜色
        Utils.setStatusTextColor(true,this)

        val wxWithdrawFragment = if (savedInstanceState != null) supportFragmentManager.findFragmentByTag(WxWithdrawFragment::class.java.name) as WxWithdrawFragment else WxWithdrawFragment()
//        wxWithdrawFragment.arguments = Bundle().apply { putBoolean("BIND", weixinBind) }
        val aliFragment = if (savedInstanceState != null) supportFragmentManager.findFragmentByTag(AliWithdrawFragment::class.java.name) as AliWithdrawFragment else AliWithdrawFragment()
        aliFragment.arguments = Bundle().apply { putBoolean("BIND", aliBind) }

        fragments.add(wxWithdrawFragment)
        fragments.add(aliFragment)
        initFragments(fragments = fragments)

        tabStates.add(TabState(weixin_text, color(R.color.weixin_color), color(R.color.withdraw_tab_unselect_color), weixin_indicator))
        tabStates.add(TabState(zhifubao_text, color(R.color.zhifubao_color), color(R.color.withdraw_tab_unselect_color), zhifubao_indicator))

        weixin_layout.setOnClickListener { activeTab(0) }

        zhifubao_layout.setOnClickListener { activeTab(1) }

        detail_about.setOnClickListener {
            quickStartActivity(WithDrawRecordFragment::class.java)
        }

        navBack.setOnClickListener { onBackPressed() }
        updateDiamond()
    }

    private fun initFragments(fragments: ArrayList<Fragment>) {

        val fragmentTransaction = supportFragmentManager.beginTransaction()

        for (f in fragments) {
            if (f.isAdded.not()) {
                fragmentTransaction.add(R.id.my_detail_fragment_container, f, f.javaClass.name)
                fragmentTransaction.hide(f)
            } else fragmentTransaction.hide(f)

        }

        fragmentTransaction.show(fragments.first())

        fragmentTransaction.commit()
    }

    fun activeTab(index: Int) {

        tabStates.forEachIndexed { i, v ->

            if (index == i) v.active()
            else v.deactive()
        }
        supportFragmentManager.beginTransaction()
                .show(fragments[index])
                .hide(fragments[1 - index])
                .commit()
    }

    fun updateDiamond() {
        val disposable = userPresenter.getDiamondAndBeanInfo()
        addToDiposable(disposable)
    }

    @SuppressLint("SetTextI18n")
    override fun onLoadDaimondBeanInfo(diamondBeanInfo: DiamondBeanInfo) {
        cashCount = diamondBeanInfo.diamond.toInt() / 10000f
        rmb_value.text = "(约" + (cashCount) + "元人民币)"
        diamondNumber.text = diamondBeanInfo.diamond
        StringHolder.diamondNumber = diamondBeanInfo.diamond
        StringHolder.qudouNumber = diamondBeanInfo.bean
        EventBus.getDefault().post(DiamondChangeEvent(diamondBeanInfo.diamond))
    }


    fun checkCashEnough(cash: String): Boolean {
        return if (cashCount > cash.toInt())
            true
        else {
            SimpleDialogBuilder()
                    .title("申请提现失败")
                    .titleTextColor(R.color.colorPrimary)
                    .buttonText("我知道了")
                    .content("趣钻余额不足，加油哦！")
                    .build(this)
                    .show()
            false
        }
    }

    inner class TabState(val nameView: TextView, val activeColor: Int, val deactiveColor: Int, val indicator: View) {


        fun active() {
            nameView.setTextColor(activeColor)
            indicator.setVisible()
        }

        fun deactive() {
            nameView.setTextColor(deactiveColor)
            indicator.setInvisible()
        }
    }
}
